package xin.yangshuai.springsecurity01.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import xin.yangshuai.springsecurity01.config.DatabaseUserDetailsManager;
import xin.yangshuai.springsecurity01.dao.SpringSecurityUserMapper;
import xin.yangshuai.springsecurity01.model.SpringSecurityUser;
import xin.yangshuai.springsecurity01.service.SpringSecurityUserService;

import java.util.List;

@Service
public class SpringSecurityUserServiceImpl implements SpringSecurityUserService {

    @Autowired
    private SpringSecurityUserMapper springSecurityUserMapper;

    @Autowired
    private DatabaseUserDetailsManager databaseUserDetailsManager;

    @Override
    public List<SpringSecurityUser> list() {
        return springSecurityUserMapper.selectByExample(null);
    }

    @Override
    public void saveUserDetais(SpringSecurityUser springSecurityUser) {

        UserDetails userDetails = User.withDefaultPasswordEncoder()
                .username(springSecurityUser.getUsername())
                .password(springSecurityUser.getPassword())
                .build();

        databaseUserDetailsManager.createUser(userDetails);
    }
}
